package kr.saeildamil.androidstudy_second27;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CopyText extends Activity {
	EditText copyEdit;
	TextView pasteText;
	ClipboardManager cm;

	Button btnSourceText;
	Button btnSourceIntent;
	Button btnTargetText;
	Button btnTargetIntent;
	Button lBtn;
	Button rBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch27_copytext);

		copyEdit=(EditText) findViewById(R.id.copyedit);
		pasteText=(TextView) findViewById(R.id.pastetext);
		cm=(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

		btnSourceText=(Button) findViewById(R.id.ch27_source_text);
		btnSourceText.setOnLongClickListener(longListener);

		btnSourceIntent=(Button) findViewById(R.id.ch27_source_intent);
		btnSourceIntent.setOnLongClickListener(longListener);

		btnTargetText=(Button) findViewById(R.id.ch27_text);
		btnTargetText.setOnDragListener(mDragListener);

		btnTargetIntent=(Button) findViewById(R.id.ch27_nodrop);
		btnTargetIntent.setOnDragListener(mDragListener);
		
		lBtn=(Button) findViewById(R.id.ch27_shadow_source);
		rBtn=(Button) findViewById(R.id.ch27_shadow_target);
		
		lBtn.setOnLongClickListener(longListener);
		rBtn.setOnDragListener(mDragListener);
	}
	View.OnDragListener mDragListener = new OnDragListener() {
		public boolean onDrag(View v, DragEvent event) {
			Button btn;
			if (v instanceof Button){
				btn = (Button) v;
			}else{
				return false;
			}
			switch(event.getAction()){
			case DragEvent.ACTION_DRAG_STARTED:
				if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
					btn.setText("Drop OK");
				}else if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)){
					btn.setText("Drop OK Intent");
				}else{
					return false;
				}
				
				break;
			case DragEvent.ACTION_DRAG_ENTERED:
				btn.setText("Enter");
				break;
			case DragEvent.ACTION_DRAG_EXITED:
				btn.setText("Exit");
				break;
			case DragEvent.ACTION_DROP:
				if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
					String text = event.getClipData().getItemAt(0).getText().toString();
					btn.setText(text);
				}else if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)){
					Intent intent = event.getClipData().getItemAt(0).getIntent();
					startActivity(intent);
					btn.setText("네이버");
				}
				break;
			case DragEvent.ACTION_DRAG_ENDED:
				if (event.getResult()){
					Toast.makeText(CopyText.this, "Drag & Drop Complete", Toast.LENGTH_LONG).show();
				}else{
					btn.setText("Target");
				}
				break;
			}
			return true;
		}
	};
	

	View.OnLongClickListener longListener=new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			switch (v.getId()) {
			case R.id.ch27_source_text:
				ClipData clip=ClipData.newPlainText("dragText", "DragText");
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				break;

			case R.id.ch27_source_intent:
				clip=ClipData.newIntent("intent", new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com")));
				break;
			case R.id.ch27_shadow_source:
				
			}
			return false;
		}
	};

	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.btncopy:
			copyText();
			break;
		case R.id.btnpaste:
			pasteText();
			break;
		case R.id.ch27_intentcopy:
			intentcopy();
			break;
		case R.id.ch27_intentpaste:
			intentpaste();
			break;
		}
	}

	private void intentpaste() {
		if(!cm.hasPrimaryClip()){
			Toast.makeText(this, "Clipboard Empty", 0).show();
			return;
		}
		if(cm.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)==false){
			Toast.makeText(this, "Clip has not intent", 0).show();
			return;
		}
		ClipData clip=cm.getPrimaryClip();
		ClipData.Item item=clip.getItemAt(0);
		Intent intent=item.getIntent();
		if(intent!=null){
			startActivity(intent);
		}
	}

	private void intentcopy() {
		Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.namer.com"));
		ClipData clip=ClipData.newIntent("intent", intent);
		cm.setPrimaryClip(clip);
	}

	private void pasteText() {
		if(!cm.hasPrimaryClip()){
			Toast.makeText(this, "Clipboard Empty", 0).show();
			return;
		}
		if(cm.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)==false){
			Toast.makeText(this, "Clip is not text", 0).show();
			return;
		}

		ClipData data=cm.getPrimaryClip();
		ClipData.Item item=data.getItemAt(0);
		pasteText.setText(item.getText());
	}

	private void copyText() {
		String str=copyEdit.getText().toString();
		if(str.length()!=0){
			ClipData clip=ClipData.newPlainText("Text", str);
			cm.setPrimaryClip(clip);
			Toast.makeText(this, "Text Copied", 0).show();
		}
	}
}
class casvasShadow extends View.DragShadowBuilder{
	int mWidth;
	int mHeight;
	Paint pnt;
	Paint pnt2;
	
	public casvasShadow(int mWidth, int mHeight) {
		super();
		this.mWidth = mWidth;
		this.mHeight = mHeight;
		init();
	}
	private void init() {
		pnt=new Paint();
		pnt.setColor(Color.YELLOW);
		
		pnt2=new Paint(Paint.ANTI_ALIAS_FLAG);
		pnt2.setColor(Color.RED);
		pnt2.setStrokeWidth(8);
		
	}
	@Override
	public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
		// TODO Auto-generated method stub
		super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
		shadowSize.set(mWidth, mHeight);
		shadowTouchPoint.set(mWidth/3, mHeight/3);
	}
	
}

